//
//  BaseViewController.swift
//  Kkmh-Swift
//
//  Created by tenltrs on 2018/10/25.
//  Copyright © 2018 Tenltrs. All rights reserved.
//

import UIKit

class BaseViewController: UIViewController {
    
    private var _statusBarHidden = false
    var statusBarHidden :  Bool {
        
        set{
            _statusBarHidden=newValue
            UIView.animate(withDuration: 0.2) {
                self.setNeedsStatusBarAppearanceUpdate()
            }
        }
        get{
            return _statusBarHidden
        }
    }
    
    override func viewDidLoad() {
        super.viewDidLoad()
        self.view.backgroundColor=UIColor.BackgroundColor
    }
    
    override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)
        
        NSCacheTool.defaultCache().setValue(self, forKey: CURRENT_VC_KEY)
        self.setNeedsStatusBarAppearanceUpdate()
        
        self.navigationController?.navigationBar.nn_backgroundViewHidden = false;
        // 设置背景颜色
        self.navigationItem.nn_backgroundColor = UIColor.NavColor
    }
    
    override func viewDidAppear(_ animated: Bool) {
        super.viewDidAppear(animated)
        
        if (self.navigationController?.viewControllers.first == self) {
            self.navigationController?.interactivePopGestureRecognizer?.isEnabled = false;
        }else{
            self.navigationController?.interactivePopGestureRecognizer?.isEnabled = true;
        }
    }
    
    
    ///下拉刷新调用
    @objc func refreshHeaderAction(header : MJRefreshHeader){
        
        logger.info(header)
    }
    
    
    /// 上拉加载调用
    @objc func refreshFooterAction(footer : MJRefreshBackGifFooter){
        
        logger.info(footer)
    }
    
    func endRefreshing(){
        self.refreshHeader.endRefreshing()
        self.refreshFooter.endRefreshing()
    }
    
    /// 取消网w洛请求
    ///
    /// - Parameter url: 请求url
    func cancelRequest(url : String) {
        
        self.sessionManager?.session.getAllTasks(completionHandler: { (tasks) in
            
            tasks.forEach({ (task) in
                if url == task.currentRequest?.url?.absoluteString{
                    logger.info("取消请求 ==== \(url)")
                    task.cancel()
                }
            })
        })
    }
 

    deinit {
        logger.info("销毁了 ==== \(String(describing: self.classForCoder))")
        
        //取消当前界面网络请求
        self.sessionManager?.session.getAllTasks(completionHandler: { (tasks) in
            tasks.forEach({ (task) in
                let url = task.currentRequest?.url?.absoluteString
                logger.info(String(format: "取消请求 ==== %@", url!))
                task.cancel()
            })
        })
    }
    
    
    // MARK: -- 懒加载
    ///头部刷新
    lazy var refreshHeader : MJRefreshNormalHeader = {
        let header = MJRefreshNormalHeader(refreshingTarget: self, refreshingAction: #selector(refreshHeaderAction(header:)))
        return header!
    }()
    
    ///底部刷新
    lazy var refreshFooter: MJRefreshBackGifFooter = {
        
        let footer = MJRefreshBackGifFooter(refreshingTarget: self, refreshingAction: #selector(refreshFooterAction(footer:)))
        footer!.ignoredScrollViewContentInsetBottom=SafeAreaInsets.bottom //记得忽略偏移量
        return footer!
    }()
    
    
    // MARK: -- 状态栏隐藏操作
    override var prefersStatusBarHidden: Bool{
        
        return self.statusBarHidden
    }
    
    override var preferredStatusBarUpdateAnimation: UIStatusBarAnimation{
        
        return UIStatusBarAnimation.slide
    }
}
